define(["utils"], function (utils) {
    function pageLogic(config) {
        this.pageview = config.pageview;
        this.nowYear = new Date().getFullYear();
        this.orderType = 0;
        this.fullPageKey = this.pageview.config.fullPageKey;
    }

    pageLogic.prototype = {
        team_dropdown_btn_click: function (sender, params) {
            // this.pageview.refs.time_dropdown_wrap.addClass("displaynone");
            this.pageview.refs.team_dropdown.$el.click();
        },
        depart_dropdown_btn_click:function(sender){
            this.pageview.refs.depart_dropdown.$el.click();
        },
        team_dropdown_btn_didmount:function(sender){
            if(this.fullPageKey ==="statistics_depart"){
                sender.$el.css({"display":"none"});
            }
        },
        depart_dropdown_btn_didmount:function(sender){
            if(this.fullPageKey !=="statistics_depart"){
                sender.$el.css({"display":"none"});
            }
        },

        type_btn_click:function(sender,params){
            this.pageview.refs.type_btn_dropdown.$el.click();
        },

        depart_dropdown_change: function (sender, params) {
            var id = params.valData.deptId||0;
            this.table.setAjaxConfigParams({deptId:id});
            this.pageview.refs.depart_dropdown_icon.setText(params.valData.deptName);
            this.table.loadPageDataByIndex(0);
        },

        team_dropdown_change: function (sender, params) {
            var id = params.valData.id||0;
            this.table.setAjaxConfigParams({groupId:id});
            this.pageview.refs.team_dropdown_icon.setText(params.valData.groupName);
            this.table.loadPageDataByIndex(0);
        },

        type_btn_dropdown_change: function (sender, params) {
            // this.table.setAjaxConfigParams({groupId: params.valData.id});
            this.pageview.refs.type_btn_icon.setText(params.valData.name);
            this.createHeader(this.pageview.refs.list_table,params.valData.name);
            this.orderType = params.valData.id;
            this.pageview.refs.list_table.setAjaxConfigParams({orderType:params.valData.id});
            this.pageview.refs.list_table.loadPageDataByIndex(0);
        },
        list_table_rowclick:function(sender,params){
            var tabledata = this.table.ajaxConfig.data;
            var isTeam = this.fullPageKey !=="statistics_depart";
            this.pageview.showPage({pageKey:"statisticsdialog",params:{
                isTeam:isTeam,
                templateId:tabledata.templateId,
                departid_or_teamid:isTeam?sender.datasource.groupId:sender.datasource.deptId,
                years:tabledata.years,
                orderType:tabledata.orderType
            }});
        },
        NumArr:["一","二","三","四","五","六","七","八","九","十","十一","十二"],
        excel_btn_click: function (sender, params) {
            this.pageview.refs.excel_dropdown.$el.click();

        },
        createHeader: function (sender,type) {
            this.timetype = type;
            var columns=[
                {
                  headerText:this.fullPageKey==="statistics_depart"?"部门":"团队",
                  cell:["row_depart_name"],
                  headerStyle:{
                    width:"160px",
                    textAlign:"left",
                    textIndent:"10px"
                  },
                  cellStyle:{

                  }
              }];
              this.pageview.config.components.row_depart_name = {
                  type: "text",
                  style: {
                      textAlign:"left",
                      textIndent:"10px"
                  },
                  text_bind: this.fullPageKey==="statistics_depart"?"deptName":"groupName"
              };
              var count = type ==="按月统计"?12:4;
              var label = type ==="按月统计"?"月":"季度";
            for (var i = 0; i < count; i++) {
                var rowWidth = 50;

                var com_key = "text_" + i;
                columns.push({
                    headerText: this.NumArr[i]+label,
                    cell: [com_key],
                    headerStyle: {
                        width: rowWidth
                    }

                });
                this.pageview.config.components[com_key] = {
                    type: "text",
                    style: {
                        textAlign:"center"
                    },
                    text_bind: "field"+(i+1)
                };
            }

            columns.push({
                  headerText:"汇总",
                  cell:["sumlabel"],
                  headerStyle:{
                    width:"80px"
                  },
                  cellStyle:{

                  }
             });
             this.pageview.config.components.sumlabel = {
                 type: "text",
                 style: {
                     textAlign:"center"
                 },
                 text_bind: "sum"
             };

            //  columns.push({
            //      headerText: "",
            //      cell: []
            //  });

            sender.createHeader(columns);
        },
        time_switch_value_init: function (sender, params) {
            var _this = this;
            this.time_switch_value = sender;
            this._bind_time_switch_value(this.searchData, true);
            this.pageview.delegate("list_table",function(target){
                _this.createHeader(target,"按月统计");
                var ajaxParams = {years:2017,orderType:_this.orderType,templateId:0};
                if(_this.fullPageKey==="statistics_depart"){
                    ajaxParams.deptId = 0;
                }else{
                    ajaxParams.groupId = 0;
                }
                target.setAjaxConfigParams(ajaxParams);
                target.loadPageDataByIndex(0);
            });
        },
        Arr2Dict:function(arr){
            var re = {};

            for(var i=0,j=arr.length;i<j;i++){
                var key =arr[i].month||arr[i].quarter;
                re[key] = arr[i].num;
            }
            return re;
        },
        list_table_parsedata:function(sender,params){
            if (params.data.code !== 0) {
                return false;
            }
            var data = params.data.data||[];
            var count = this.timetype==="按月统计"?12:4;
            for(var i=0,j=data.length;i<j;i++){
                var resultList = data[i].resultList;
                var dict = this.Arr2Dict(resultList);
                var sum = 0;
                for(ii=1;ii<=count;ii++){
                    data[i]["field"+ii] = dict[ii.toString()]||0;
                    sum+=data[i]["field"+ii];
                }
                data[i].sum = sum;
            }
            return {data:data,sumCount:0};
        },

        time_switch_lefticon_click: function (sender, params) {
            this.searchData = utils.getPreYear(this.searchData);
            this._bind_time_switch_value(this.searchData);
        },
        time_switch_righticon_init: function (sender, params) {
            this.time_switch_righticon = sender;
            sender.setDisabled(true);
        },
        time_switch_righticon_click: function (sender, params) {
            this.searchData = utils.getNextYear(this.searchData);
            this._bind_time_switch_value(this.searchData);
        },

        _bind_time_switch_value: function (date, isInit) {
            var searchDataInfo = utils.getDateInfo(date);
            var text = searchDataInfo.year + "年" ;
            var dateCondition = searchDataInfo.year;
            if (isInit === true) {
                this.time_switch_value.config.text = text;
            } else {
                this.time_switch_value.setText(text);
            }
            if (this.time_switch_righticon) {
                var disabled = searchDataInfo.year >= this.nowYear;
                if(disabled){
                    this.time_switch_righticon.$el.addClass("switch-disabled");
                }else{
                    this.time_switch_righticon.$el.removeClass("switch-disabled");
                }
                this.time_switch_righticon.setDisabled(disabled);
            }
            this.curDateCondition = dateCondition;
            if(this.table){
                this.table.setAjaxConfigParams({years:searchDataInfo.year});
                this.table.loadPageDataByIndex(0);
            }

            // this.loadData(this.curDateCondition);
        },
        excel_dropdown_change:function(sender,params){
            var url = "exportExcelIncludeUserStatisticsByCondition";
            if(params.valData.id===0){
                url = "exportExcelStatisticsByCondition";
            }
            var _this = this;
            var isTeam = this.fullPageKey !=="statistics_depart";
            var tabledata = this.table.ajaxConfig.data;
            var token = this.pageview.params.token;
            var deptIdOrtemplate = isTeam? ("&groupId=" + tabledata.groupId):("&deptId=" + tabledata.deptId);
            url = "/diaryQuery/"+url+"?timestamp="+ (new Date()).valueOf()+ "&token=" + token+ "&orderType=" + tabledata.orderType+ deptIdOrtemplate+ "&years=" + tabledata.years+ "&templateId=" + tabledata.templateId;
            url = utils.getUrl(url);
            window.open(url);
        },
        excel_dropdown_getdata:function(sender,params){
            var data = [
                {id:0,name:"仅团队数据"},
                {id:1,name:"团队和人员数据"},
            ];
            if(this.fullPageKey==="statistics_depart"){
                data = [
                    {id:0,name:"仅部门数据"},
                    {id:1,name:"部门和人员数据"},
                ];
            }
            params.success(data);
        },
        type_btn_dropdown_getdata:function(sender,params){
            params.success([
                {id:0,name:"按月统计"},
                {id:1,name:"按季度统计"},
            ]);
        },
        depart_dropdown_getdata:function(sender,params){
            if(this.fullPageKey !=="statistics_depart"){
                params.success([]);
                return;
            }

            this.pageview.ajax({
                type: "GET",
                url: "/team/getAuthDepts",
                data: {},
                success: function (data) {
                    if (data.code === 0) {

                        var re = [{
                            gid: null,
                            deptId: 0,
                            deptName: "全部部门"
                        }].concat(data.data);

                        params.success(re);
                    } else {
                        params.error();
                    }
                },
                error: function (e) {
                    params.error();
                }
            });
        },

        team_dropdown_getdata: function (sender, params) {
            if(this.fullPageKey ==="statistics_depart"){
                params.success([]);
                return;
            }
            this.pageview.ajax({
                type: "GET",
                url: "/group/authMe",
                data: {},
                success: function (data) {
                    if (data.code === 0) {

                        var re = [{
                            gid: null,
                            id: null,
                            groupName: "全部团队"
                        }].concat(data.data);

                        params.success(re);
                    } else {
                        params.error();
                    }
                },
                error: function (e) {
                    params.error();
                }
            });

        },

        temp_dropdown_btn_click: function (sender) {
            // this.pageview.refs.time_dropdown_wrap.addClass("displaynone");
            this.pageview.refs.temp_dropdown.$el.click();
        },
        list_table_init:function(sender,params){
                this.table = sender;
        },

        temp_dropdown_change: function (sender, params) {

            // if (params.valData.id === 0) {
            //     this.table.deleteAjaxConfigParams({templateId: null});
            // } else {
            // }

            this.table.setAjaxConfigParams({templateId: params.valData.id});

            this.pageview.refs.temp_dropdown_icon.setText(params.valData.title);
            this.table.loadPageDataByIndex(0);
        },

        temp_dropdown_getdata: function (sender, params) {
            this.pageview.ajax({
                type: "GET",
                url: "/template/list",
                data: {
                    pageNumber: 1,
                    pageSize: 1000,
                    client: "app"
                },
                success: function (data) {
                    if (data.code === 0) {
                        var dataArr = [{id: 0, title: "全部模版"}].concat(data.data);
                        params.success(dataArr);
                    } else {
                        params.error();
                    }
                }, error: function () {
                    params.error();
                }
            });
        },
    };
    return pageLogic;
});
